import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;


public class DialogCreerSalon extends JDialog {
	private JTextField textFieldNom;
	private JTextField textFieldNbPlaces;
	private JPasswordField passwordField;
	private Client controleur;
	private JLabel labelPassword;
	private JTextArea textAreaDescription;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	public DialogCreerSalon(Client client) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fermerFenetre();
			}
		});
		setModal(true);
		this.controleur = client;
		this.setBounds(150,150,516,303);
		setTitle("Créer un Salon");
		getContentPane().setLayout(null);
		
		JLabel label = new JLabel("Nom");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setBounds(12, 14, 160, 15);
		getContentPane().add(label);
		
		textFieldNom = new JTextField();
		textFieldNom.setColumns(10);
		textFieldNom.setBounds(190, 12, 306, 19);
		getContentPane().add(textFieldNom);
		
		JRadioButton radioButtonPublic = new JRadioButton("Public");
		buttonGroup.add(radioButtonPublic);
		radioButtonPublic.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				activationPublic();
			}
		});
		radioButtonPublic.setSelected(true);
		radioButtonPublic.setHorizontalAlignment(SwingConstants.LEFT);
		radioButtonPublic.setBounds(12, 37, 69, 23);
		getContentPane().add(radioButtonPublic);
		
		JRadioButton radioButtonPrive = new JRadioButton("Privé");
		buttonGroup.add(radioButtonPrive);
		radioButtonPrive.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				activationPrive();
			}
		});
		radioButtonPrive.setHorizontalAlignment(SwingConstants.LEFT);
		radioButtonPrive.setBounds(12, 64, 85, 23);
		getContentPane().add(radioButtonPrive);
		
		this.labelPassword = new JLabel("Mot de passe");
		labelPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		labelPassword.setEnabled(false);
		labelPassword.setBounds(12, 95, 160, 15);
		getContentPane().add(labelPassword);
		
		JLabel label_2 = new JLabel("Description");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(12, 122, 160, 15);
		getContentPane().add(label_2);
		
		this.textAreaDescription = new JTextArea();
		textAreaDescription.setBounds(190, 122, 306, 67);
		getContentPane().add(textAreaDescription);
		
		JLabel label_3 = new JLabel("Nombre de places");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(12, 201, 160, 15);
		getContentPane().add(label_3);
		
		textFieldNbPlaces = new JTextField();
		textFieldNbPlaces.setColumns(10);
		textFieldNbPlaces.setBounds(190, 199, 51, 19);
		getContentPane().add(textFieldNbPlaces);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setBounds(247, 235, 249, 27);
		getContentPane().add(splitPane);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fermerFenetre();
			}
		});
		splitPane.setLeftComponent(btnAnnuler);
		
		JButton btnCreer = new JButton("Créer");
		btnCreer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				creerSalon();
			}
		});
		splitPane.setRightComponent(btnCreer);
		
		passwordField = new JPasswordField();
		passwordField.setEnabled(false);
		passwordField.setBounds(190, 93, 306, 19);
		getContentPane().add(passwordField);
	}
	
	public void fermerFenetre()
	{
		this.controleur.fermerDialogCreerSalon();
	}
	
	public void activationPublic()
	{
		labelPassword.setEnabled(false);
		passwordField.setEnabled(false);
		passwordField.setText("");
	}
	
	public void activationPrive()
	{
		labelPassword.setEnabled(true);
		passwordField.setEnabled(true);
	}
	
	public void creerSalon() {
		String nomSalon = textFieldNom.getText();
		String mdp = new String (this.passwordField.getPassword());
		String description = textAreaDescription.getText();
		int nbPlaces = 0;

		try {
			nbPlaces = Integer.parseInt(textFieldNbPlaces.getText());
		}
		catch (NumberFormatException e)
		{
			JOptionPane.showMessageDialog(this, "Le champ Nb places doit être un entier !", "Erreur champ Nb places", JOptionPane.ERROR_MESSAGE);
		}
		
		if (this.controleur.creerSalon(nomSalon, mdp, description, nbPlaces)) 
		{
			JOptionPane.showMessageDialog(this, "Le salon " + nomSalon + " a bien été créé.", "Confirmation création salon", JOptionPane.INFORMATION_MESSAGE);
			this.controleur.fermerDialogCreerSalon();
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Le salon n'a pas pu être créé !", "Erreur création salon", JOptionPane.ERROR_MESSAGE);
		}
		
	}
}
